﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;

namespace SDLITS.Web.Basic
{
    public partial class RoomManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("RoomChange.aspx", "新增区域");
                BindGrid(GetWhere());
            }
        }
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = SDDbSession.Default.From<V_RoomCompany>().Where(wc).Count();
            List<V_RoomCompany> items = SDDbSession.Default.From<V_RoomCompany>().Where(wc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();
            Grid1.DataSource = items;
            Grid1.DataBind();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }
        protected WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!string.IsNullOrEmpty(Request["aid"]))
            {
                wc = wc && V_RoomCompany._.AreaID == Request["aid"];
            }
            if (!string.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && V_RoomCompany._.RoomName.Contain(ttbxMyBox2.Text.Trim());
            }
            return wc;
        }
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                Grid1.PageIndex = 0;
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            Grid1.PageIndex = 0;
            BindGrid(GetWhere());
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                string roomStatus = Grid1.Rows[e.RowIndex].Values[3].ToString();
                if (roomStatus.Contains("已使用"))
                    Alert.Show("房间正在使用，不能删除。");
                else
                {
                    SDDbSession.Default.Delete<Room>(Room._.RoomID == id);
                    BindGrid(GetWhere());

                }
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            string status = e.Values[3].ToString();
            switch (status)
            {
                case "1": e.Values[3] = "<font color='green'>未使用</font>"; break;
                case "2": e.Values[3] = "<font color='blueviolet'>已使用</font>"; break;
                default: e.Values[3] = "<font color='red'>异 常</font>"; break;
            }
        }
    }
}
